<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <script>
        // 代理+ 反射
        let formData = {
            account: 'admin',
            password: '123456',
            email: "",
            phone: ""
        };
        // 账号：6~20
        // 密码：6~20
        // 作业1：账号\密码至少含有字母+数字
        var reg = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,20}$/;
        // console.log(reg.test("abcaaasda"));
        let handel = {
            set: function (target, key, value) {
                //key要准备修改的属性名称
                //判断为账号acocunt，密码password
                if (key == 'account' || key == 'password') {
                    if (!reg.test(value)) {
                        return;
                    }
                }
                //判断为邮箱email
                if (key == 'email') {
                    if (value.length < 7 || value.length > 20) {
                        return;
                    }
                }
                //判断为手机号phone
                if (key == 'phone') {
                    if (value.length != 11) {
                        return;
                    }
                }
                target[key] = value;
            },
            get: function (target, key) {
                console.log('get函数被调用：', target, key)
                return target[key];
            }
        }
        formData = new Proxy(formData, handel);

        // Reflect.set(formData, 'account', 'admin123');
        Reflect.set(formData, 'account', '123'); //为什么没有变为123？
        Reflect.set(formData, 'account', 'sdafasdfs12378');
        Reflect.set(formData, 'password', 'sda56488');
        Reflect.set(formData, 'phone', '18530796269');
        Reflect.set(formData, 'email', '2497232834@qq.com');
        // let {account,password} = formData;        
        let account = Reflect.get(formData, 'account');
        let password = Reflect.get(formData, 'password');
        let phone = Reflect.get(formData, 'phone');
        let email = Reflect.get(formData, 'email');
        console.log(account, password, phone, email);
    </script>
</body>

</html>